home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / threads / nttimer.c < prev    next >
C/C++ Source or Header  |  1996-02-04  |  2KB  |  109 lines

  1.  
  2.  
  3. /*
  4.  *
  5.  *    Copyright (c) 1995-1996 Algorithms Corporation
  6.  *    3020 Liberty Hills Drive
  7.  *    Franklin, TN  37067
  8.  *
  9.  *    ALL RIGHTS RESERVED.
  10.  *
  11.  *
  12.  *
  13.  */
  14.  
  15.  
  16. #if defined(_MSC_VER)
  17. #if !defined(WIN32)  &&  _MSC_VER >= 900
  18. #define    WIN32
  19. #endif
  20. #endif
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <windows.h>
  25. #include <mmsystem.h>
  26.  
  27.  
  28. #define    CONTEXT_SWITCHES_PER_SECOND    50
  29.  
  30.  
  31.  
  32.  /*  thousands of a second  */
  33.  
  34. #define    TARGET_MILLSECS    (1000/CONTEXT_SWITCHES_PER_SECOND) 
  35.  
  36.  
  37.  
  38. #ifdef TEST
  39. int    _tick_count=10000;
  40. #else
  41. extern    int    _tick_count;
  42. #endif
  43.  
  44. static    UINT        wTimerRes;
  45. static    UINT        id;
  46.  
  47. static    void    CALLBACK
  48. timerfun(UINT    idEvent,
  49.      UINT    uReserved,
  50.      DWORD    dwUser,
  51.      DWORD    rwReserved1,
  52.      DWORD    rwReserved2)
  53. {
  54.     if (_tick_count)
  55.         _tick_count--;
  56. }
  57.  
  58. static    void    _end_timer(void)
  59. {
  60.     timeKillEvent(id);
  61.     timeEndPeriod(wTimerRes);
  62. }
  63.  
  64. void    _start_timer()
  65. {
  66.     TIMECAPS    tc;
  67.  
  68.     timeGetDevCaps(&tc, sizeof(TIMECAPS));
  69.     wTimerRes = min(max(tc.wPeriodMin, TARGET_MILLSECS), tc.wPeriodMax);
  70.     timeBeginPeriod(wTimerRes);
  71.     id = timeSetEvent(wTimerRes, wTimerRes, (LPTIMECALLBACK) timerfun, (DWORD) 0, TIME_PERIODIC);
  72.     atexit(_end_timer);
  73. }
  74.  
  75. #ifdef TEST
  76.  
  77. main()
  78. {
  79.     int    n;
  80.     long    i;
  81.  
  82.     _start_timer();
  83.  
  84.     for (n=0 ; n++ != 10 ; )  {
  85.         fprintf(stderr, "Timer = %d\n", _tick_count);
  86.         for (i=0L ; i++ != 1000000L ; );
  87.     }
  88.     return(0);
  89. }
  90.  
  91. #endif
  92.  
  93.  
  94.  
  95. /*
  96.  *
  97.  *    Copyright (c) 1995-1996 Algorithms Corporation
  98.  *    3020 Liberty Hills Drive
  99.  *    Franklin, TN  37067
  100.  *
  101.  *    ALL RIGHTS RESERVED.
  102.  *
  103.  *
  104.  *
  105.  */
  106.  
  107.  
  108.  
  109.